package com.dhecp.project.base.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.base.domain.OperatorBankInfo;
import com.dhecp.project.base.mapper.OperatorBankInfoMapper;
import com.dhecp.project.base.service.OperatorBankInfoService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 合作商账户 服务层
 * @author DH-Xxj
 * @version 2022年10月13日
 */
@Service
public class OperatorBankInfoServiceImpl implements OperatorBankInfoService {

	@Autowired
	private OperatorBankInfoMapper dataInfoMapper;

	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;

	/**
	 * 查询
	 * @param params
	 * @return List - OperatorBankInfo
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
	public List<OperatorBankInfo> queryData(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));		//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));		//数据权限查询条件SQL语句
		PageUtils.startPage();
		return dataInfoMapper.findData(params);
	}

	/**
	 * 通过guid查询
	 * @param guid 必要参数：行唯一值
	 * @return OperatorBankInfo 实体
	 * @author DH-Xxj
	 */
	@Override
	public OperatorBankInfo queryDataByGuid(Map<String, Object> params) {
		return dataInfoMapper.findDataByGuid(params);
	}

    /**
     * 新增
     * @param infoItems 必要参数：对应实体类的json格式字符串
     * @return
     * @author DH-Xxj
     */
	@Override
	public AjaxResult insertData(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<OperatorBankInfo> dataInfoList = JSON.parseArray(params.get("infoItems").toString(), OperatorBankInfo.class);
		
		if (dataInfoList == null || dataInfoList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (OperatorBankInfo dataInfo : dataInfoList) {
			dataInfo.setCreateTime(new Date());
			dataInfo.setUpdateTime(new Date());
			dataInfo.setcUser(ShiroUtils.getUserId());
			dataInfo.setcUserName(ShiroUtils.getUserName());
			dataInfo.setcDept(ShiroUtils.getUserDeptPath());
			dataInfo.setcDeptName(ShiroUtils.getUserDeptName());
			dataInfo.setcPosition(ShiroUtils.getUserPositionName());
			
			dataInfoMapper.insertData(dataInfo);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 删除
	 * @param infoItems 必要参数：对应实体类的json格式字符串
	 * @return
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult deleteData(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<OperatorBankInfo> dataInfoList = JSON.parseArray(params.get("infoItems").toString(), OperatorBankInfo.class);
		if (dataInfoList == null || dataInfoList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (OperatorBankInfo dataInfo : dataInfoList) {
			dataInfoMapper.deleteData(dataInfo);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 通过guid修改数据
	 * @param infoItems 必要参数：对应实体类的json格式字符串
	 * @return
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult updateByGuid(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<OperatorBankInfo> dataInfoList = JSON.parseArray(params.get("infoItems").toString(), OperatorBankInfo.class);
		if (dataInfoList == null || dataInfoList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (OperatorBankInfo dataInfo : dataInfoList) {
			dataInfo.setUpdateTime(new Date());
			dataInfoMapper.updateByGuid(dataInfo);
		}
		return AjaxResult.success("操作成功");
	}
	
}
